package com.example.camunda.rest;


import com.example.camunda.vo.CompleteTaskVo;
import com.example.camunda.vo.DelegateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.camunda.bpm.engine.rest.TaskRestService;
import org.camunda.bpm.engine.rest.dto.task.TaskDto;
import org.camunda.bpm.engine.rest.dto.task.TaskQueryDto;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 任务待办
 */
@Api(tags = "四、任务待办")
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    TaskService taskService;
    @Autowired
    HistoryService historyService;

    //@Resource
    //TaskRestService taskRestService;

    private  RuntimeService runtimeService;

    @Autowired
    IdentityService identityService;

    @GetMapping("/findPage/")
    @ApiOperation("查询任务列表")
    public    List<TaskDto> findPage(@RequestParam TaskQueryDto taskQueryDto, @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize) {
       // return taskRestService.queryTasks(taskQueryDto,pageNum,pageSize);
        return null;
    }


    @GetMapping("/{userId}")
    @ApiOperation("查询我的待办")
    public  List<Task> get(@PathVariable String userId) {
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(userId).list();
        return taskList;
    }


    @GetMapping("/findMyUndoTask/{processBusinessKey}")
    @ApiOperation("根据业务Key查询任务列表")
    public  List<Task> findBusinessKey(@PathVariable String processBusinessKey) {
        List<Task> taskList = taskService.createTaskQuery().processInstanceBusinessKey(processBusinessKey).list();
        return taskList;
    }


    @GetMapping("/findByProcessId/{processInstanceId}")
    @ApiOperation("按照流程实例查询任务列表")
    public  List<Task> findByProcessId(@PathVariable String processInstanceId) {
        List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
        return taskList;
    }

   /* @GetMapping("/list/{processDefinitionKey}")
    @ApiOperation("查询任务列表")
    public Page<Task> list(@PathVariable("processDefinitionKey")String processDefinitionKey,@RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        TaskQuery taskQuery=taskService.createTaskQuery();
        long count=taskQuery.count();
        List<Task> taskList = taskQuery.processDefinitionKey(processDefinitionKey).listPage(pageNum * pageSize, pageSize);
        //List<TaskResultDto> dtoList = taskList.stream().map(TaskResultDto::from).collect(Collectors.toList());
        return new PageImpl<>(taskList, PageRequest.of(pageNum, pageSize), count);
    }*/

    @GetMapping("/findMyHistoryTask/{userId}")
    @ApiOperation("查询我的已办")
    public  List<HistoricTaskInstance> findMyHistoryTask(@PathVariable String userId) {
        List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).finished().list();
        return historicTaskInstances;
    }

    @PostMapping("/completeTask")
    @ApiOperation("审批通过")
    public  void completeTask(@RequestBody CompleteTaskVo completeTaskVo) {
        //添加审批人
        identityService.setAuthenticatedUserId(completeTaskVo.getAssignee());
        //添加审批意见，可在Act_Hi_Comment里的message查询到
        //三个参数分别为待办任务ID,流程实例ID,审批意见
        taskService.createComment(completeTaskVo.getTaskId(), completeTaskVo.getProcessInstanceId(), completeTaskVo.getComment());
        //任务完成  也就是审批通过
        taskService.complete(completeTaskVo.getTaskId());
    }

    // taskService.delegateTask(taskId, assignee);

    @PostMapping("/delegateTask")
    @ApiOperation("审批转办")
    public  void completeTask(@RequestBody DelegateVo delegateVo) {
        taskService.delegateTask(delegateVo.getTaskId(), delegateVo.getAssignee());
    }



}
